package com.tn.service.rpm.mq;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.tn.controller.alarm.common.model.rpm.mq.EventMQDTO;
import com.tn.mqtt.AbstractBaseCallback;
import com.tn.service.tallying.TallyingService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class TofDisCheckMQ extends AbstractBaseCallback {
    private final static String topic = "rpm_serve/async_tof";
    @Autowired
    private TallyingService tallyingService;

    @Override
    public void consumer(String topic, String msg) {
        log.info("topic:{}, msg:{}", topic, msg);
        if (StringUtils.isBlank(msg)) {
            log.error("RpmAsyncMQ msg empty {}", msg);
            return;
        }
        //TODO业务逻辑
        JSONObject msgJson = JSON.parseObject(msg);
        if (null == msgJson || null == msgJson.getJSONObject("event")) {
            log.error("RpmAsyncMQ eventMQDTORpmMqDTO or eventMQDTORpmMqDTO.getEvent empty msg：{}", msg);
            return;
        }
        tallyingService.tallyingHandle(JSON.parseObject(msgJson.getJSONObject("event").toString(),
                new TypeReference<EventMQDTO>() {
        }));
    }

    @Override
    protected AbstractBaseCallback getClazz() {
        return this;
    }

    @Override
    protected String getTopic() {
        return topic;
    }
}
